home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter Code.exe / Chapter08 / TranslatePanel.java < prev    next >
Text File  |  2000-06-26  |  2KB  |  84 lines

  1.  
  2.  
  3.  
  4. package applets;
  5.  
  6. import shout3d.*;
  7. import shout3d.core.*;
  8. import shout3d.math.*;
  9.  
  10.  
  11. public class TranslatePanel extends Shout3DPanel implements DeviceObserver{
  12.     
  13.     Transform boxTrans;
  14.     float worldPosX;
  15.     int pixelStartX;
  16.     int pixelEndX;
  17.  
  18.  
  19.  
  20.     public TranslatePanel (Shout3DApplet applet){
  21.         super(applet);
  22.     }
  23.     
  24.         public void customInitialize() {
  25.         addDeviceObserver(this,"MouseInput", null);
  26.  
  27.         //get reference to the Transform node you need
  28.         boxTrans = (Transform) getNodeByName("mytrans");
  29.  
  30.         //get the x position value from the transform
  31.         //and store it in the worldPosX variable
  32.         worldPosX = boxTrans.translation.getValue()[0];
  33.  
  34.         //print out the value in worldPosX
  35.         //to confirm that starting x value is 0
  36.         System.out.println("Box's x position is " + worldPosX);        
  37.     }
  38.  
  39.  
  40.  
  41.     protected void finalize()  { 
  42.         removeDeviceObserver(this,"MouseInput");
  43.  
  44.     }
  45.  
  46.  
  47.     public boolean onDeviceInput(DeviceInput di, Object userData) {
  48.         MouseInput mi = (MouseInput) di;
  49.  
  50.         switch (mi.which){
  51.             case MouseInput.DOWN:
  52.                 pixelStartX = mi.x;
  53.                 return true;                                        
  54.  
  55.             case MouseInput.DRAG:
  56.                 pixelEndX = mi.x;
  57.                 
  58.                 //get pixel distance dragged
  59.                 int dragDistance = pixelEndX - pixelStartX;
  60.  
  61.                 //convert pixel distance to meters
  62.                 // at 100 pixels/1 meter
  63.                 float deltaX = dragDistance/100f;
  64.  
  65.                 //add delta to current X position 
  66.                 //to get new position                
  67.                 worldPosX = worldPosX + deltaX;
  68.  
  69.                 //put the new X position
  70.                 //in the transform node.
  71.                 boxTrans.translation.set1Value(0, worldPosX);
  72.  
  73.                 //reset the starting pixel for next drag
  74.                 pixelStartX = pixelEndX;
  75.                 
  76.                 return true;
  77.  
  78.         }//end of switch
  79.  
  80.         return false;        
  81.     }
  82.  
  83.  
  84. } //end of class